from rest_framework import serializers
from apps.books.models import BookInfo, Category, BookHistory


class CategoryModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = '__all__'
        read_only_fields = ['id']


class BookInfoModelSerializer(serializers.ModelSerializer):
    category_id = serializers.IntegerField(label='类别id')
    category = serializers.StringRelatedField()

    class Meta:
        model = BookInfo
        fields = '__all__'
        read_only_fields = ['id']


class HistoryModelSerializer(serializers.ModelSerializer):
    book_id = serializers.IntegerField(label='书籍id')
    book = BookInfoModelSerializer()
    user_id = serializers.IntegerField(label='用户id')
    user = serializers.StringRelatedField()

    class Meta:
        model = BookHistory
        fields = '__all__'
        read_only_fields = ['id', 'book', 'user']
